import java.util.List;

/*
 * @lc app=leetcode.cn id=872 lang=java
 *
 * [872] 叶子相似的树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        List<Integer> t1 = new ArrayList<>();
        List<Integer> t2 = new ArrayList<>();

        preOrder(root1,t1);
        preOrder(root2,t2);
        if(t1.size()==t2.size()){
            for(int i = 0 ;i<t1.size();i++){
                if(t1.get(i)!=t2.get(i)){
                    return false;
                }
            }
        }else return false;
        
        return true;
    }
    //先序遍历，注意空指针问题。
    public void preOrder(TreeNode root,List<Integer> t){
        if(root == null){
            return;
        }
        if(root.left==null && root.right==null){
            t.add(root.val);
        }
        preOrder(root.left,t);
        preOrder(root.right,t);
    }
}
// @lc code=end

